﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Interfaces;
using System.Drawing;
using Silvermoon.OpenGL.Native;
using Silvermoon.Controls;

namespace Silvermoon.Shapes
{
    /// <summary>
    /// Shape of a bubble with a drop shadow.
    /// </summary>
    public class ShadowedBubbleShape : Shape, IColor, IBubble
    {
        private BubbleShape shape;

        public ShadowedBubbleShape()
            : base()
        {
            shape = new BubbleShape();
        }

        public override void Dispose()
        {
            base.Dispose();
            shape.Dispose();
        }

        public RenderStyle RenderStyle
        {
            get { return shape.RenderStyle; }
            set
            {
                shape.RenderStyle = value;
            }
        }

        public int TileCount
        {
            get { return shape.TileCount; }
            set
            {
                shape.TileCount = value;
                Invalidate();
            }
        }

        public int SelectedTile
        {
            get { return shape.SelectedTile; }
            set
            {
                shape.SelectedTile = value;
                Invalidate();
            }
        }

        public Color SelectedColor
        {
            get { return shape.SelectedColor; }
            set
            {
                shape.SelectedColor = value;
                Invalidate();
            }
        }

        public Placement ArrowPlacement
        {
            get { return shape.ArrowPlacement; }
            set
            {
                shape.ArrowPlacement = value;
                Invalidate();
            }
        }

        public int ArrowOffset
        {
            get { return shape.ArrowOffset; }
            set
            {
                shape.ArrowOffset = value;
                Invalidate();
            }
        }

        public override Color Color
        {
            get { return shape.Color; }
            set
            {
                shape.Color = value;
                Invalidate();
            }
        }

        public int ArrowSize
        {
            get { return shape.ArrowSize; }
        }

        protected override void Layout()
        {
            base.Layout();
            shape.Bounds = Bounds;
            shape.PreLoad();
        }

        public override void Render(Silvermoon.Core.Renderer renderer)
        {
            shape.PreLoad();
            bool isShadowed = renderer.Shadowed;
            if (!isShadowed)
            {
                int selectedTile = shape.SelectedTile;
                shape.SelectedTile = -1;
                renderer.Shadowed = true;

                shape.Location = Location;
                Rectangle box = renderer.ScissorBox;
                renderer.ScissorBox = new Rectangle(-int.MaxValue, -int.MaxValue, int.MaxValue, int.MaxValue);
                gl.Translatef(6, 5, -0f);
                shape.Render(renderer);
                gl.Translatef(-6, -5, 0f);
                renderer.ScissorBox = box;
                renderer.Shadowed = false;
                shape.SelectedTile = selectedTile;
            }
            shape.Render(renderer);

        }


        public Rectangle GetTileRect(int index)
        {
            shape.Bounds = Bounds;
            return shape.GetTileRect(index);
        }

        #region IBubble Members


        public Rectangle GetTilesRect()
        {
            return shape.GetTilesRect();
        }

        #endregion

        protected override Shape CreateClone()
        {
            return new ShadowedBubbleShape();
        }

        protected override void Copy(Shape shape)
        {
            base.Copy(shape);
            ShadowedBubbleShape s = shape as ShadowedBubbleShape;
            s.RenderStyle = RenderStyle;
            s.TileCount = TileCount;
            s.SelectedTile = SelectedTile;
            s.SelectedColor = SelectedColor;
            s.ArrowPlacement = ArrowPlacement;
            s.ArrowOffset = ArrowOffset;
        }

        public int GetVerticalSpace()
        {
            return shape.GetVerticalSpace();
        }
    }
}
